package com.swpu.web.servlet.Brand;

import com.alibaba.fastjson.JSON;
import com.swpu.pojo.Brand;
import com.swpu.pojo.PageBean;
import com.swpu.service.BrandService;
import com.swpu.service.impl.BrandServiceImpl;
import com.swpu.web.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet {

    private BrandService brandService = new BrandServiceImpl();

    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws Exception{
        List<Brand> brands = brandService.selectAll();
        String jsonString = JSON.toJSONString(brands);

        //写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //接收前端数据
        BufferedReader br = request.getReader();
        String params = br.readLine();

        Brand brand = JSON.parseObject(params, Brand.class);
        brandService.add(brand);

        //响应成功的标识
        response.getWriter().write("add success");
    }

    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //接收前端数据
        BufferedReader br = request.getReader();
        String params = br.readLine();

        int[] ids = JSON.parseObject(params,int[].class);
        brandService.deleteByIds(ids);

        //响应成功的标识
        response.getWriter().write("delete success");
    }

    public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String currentPageSt = request.getParameter("currentPage");
        String pageSizeSt = request.getParameter("pageSize");
        int currentPage = Integer.parseInt(currentPageSt);
        int pageSize = Integer.parseInt(pageSizeSt);

        PageBean<Brand> pageBean = brandService.selectByPage(currentPage, pageSize);

        String jsonString = JSON.toJSONString(pageBean);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String currentPageSt = request.getParameter("currentPage");
        String pageSizeSt = request.getParameter("pageSize");
        int currentPage = Integer.parseInt(currentPageSt);
        int pageSize = Integer.parseInt(pageSizeSt);

        //post获取对象参数
        BufferedReader br = request.getReader();
        String params = br.readLine();
        Brand brand = JSON.parseObject(params,Brand.class);

        PageBean<Brand> pageBean = brandService.selectByPageAndCondition(currentPage, pageSize, brand);
        int totalCount = brandService.selectTotalCountByCondition(brand);

        pageBean.setTotalCount(totalCount);

        String jsonString = JSON.toJSONString(pageBean);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
}
